//
//  QRCodeViewController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/15.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "QRCodeViewController.h"
#import "ZBarSDK.h"
#import <AVFoundation/AVFoundation.h>

@interface QRCodeViewController ()<ZBarReaderDelegate>
{
    AVCaptureSession * _AVSession;
    BOOL _lightOn;
}
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (weak, nonatomic) IBOutlet UIImageView *light;

@end

@implementation QRCodeViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _lightOn = 0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    ZBarReaderController *reader = [ZBarReaderController new];
//    reader.readerDelegate = self;
//    ZBarImageScanner *scanner = reader.scanner;
//    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
//    
//    reader.showsZBarControls = YES;
//    
//    [self presentViewController:reader animated:YES completion:^{
//        
//    }];
//
    _light.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [_light addGestureRecognizer:tap];
}


//
//-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    id<NSFastEnumeration>results = [info objectForKey:ZBarReaderControllerResults];
//    ZBarSymbol *symbol;
//    for (symbol in results) {
//        break;
//    }
//    _imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    
////    NSString *结果 = symbol.data;
//
//}
//

-(void)tap
{
    _lightOn = !_lightOn;
    if (_lightOn) {
        [self turnOnLed];
    }else{
        [self turnOffLed];
    }
}



//关闭闪光灯
-(void)turnOffLed {
    _light.image = [UIImage imageNamed:@"sweep_flashUn"];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}

//开启闪光灯
-(void)turnOnLed {
    _light.image = [UIImage imageNamed:@"sweep_flashOn"];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: AVCaptureTorchModeOn];
        [device unlockForConfiguration];
    }   
}


- (IBAction)back:(id)sender {
    [self  turnOffLed];
    [self.navigationController popViewControllerAnimated:YES];
}



@end
